interface FloderFilesResult {
  filename: string;
  resolveFilePath: string;
}

//获取指定context下的文件列表
export const getFloderFiles: (
  files: __WebpackModuleApi.RequireContext
) => Array<FloderFilesResult> = function(files) {
  const res: Array<FloderFilesResult> = [];
  files.keys().forEach((item) => {
    const resolveFilePath: string = item.replace("./", "");
    const pArr = item.split("/");
    const name: string = pArr[pArr.length - 1].replace(/\.(vue|js|ts)$/, "");
    const r: FloderFilesResult = {
      filename: name,
      resolveFilePath,
    };
    res.push(r);
  });
  return res;
};

//数字转化
export const formatNumber: (number: number) => number | string = function(
  number
) {
  if (number > 10000) {
    return parseFloat((number / 10000).toFixed(1)) + "万";
  }
  return number;
};

//时间挫转转化成分秒
export const formatTimeStamp: (time: number) => string = function(time) {
  const ht = 60 * 60 * 1000,
    mt = 60 * 1000,
    st = 1000;
  const h = Math.floor(time / ht);
  const m = Math.floor((time - h * ht) / mt);
  const s = Math.floor((time - h * ht - m * mt) / st);
  const sh = h < 10 ? `0${h}` : `${h}`;
  const sm = m < 10 ? `0${m}` : `${m}`;
  const ss = s < 10 ? `0${s}` : `${s}`;
  return `${sm}:${ss}`;
};

/**
 * 函数节流
 * @param {Function} func 需要被节流的函数
 * @param {Number} wait 节流的时间间隔 毫秒
 */
export const throttle: (func: Function, wait?: number) => any = function(
  func,
  wait = 300
) {
  let timeout;
  return function(...arg) {
    if (!timeout) {
      timeout = setTimeout(() => {
        timeout = null;
        func(...arg);
      }, wait);
    }
  };
};

/**
 * 获取文件名
 */
export function getFileName(name: string): string {
  const p = name.substring(0, name.lastIndexOf("."));
  return p.split("/").pop() || "";
}
